package cn.maihe.elg.operation.supports.electronicseal.anne.bean.annesign.base;

import cn.maihe.elg.operation.supports.electronicseal.anne.utils.annesign.AnneSignConst;
import cn.maihe.elg.operation.supports.electronicseal.anne.utils.annesign.SystemErrorCode;
import cn.maihe.elg.operation.supports.electronicseal.anne.utils.annesign.ValidateException;
import cn.maihe.elg.operation.supports.electronicseal.anne.utils.annesign.ValidateUtils;
import com.fasterxml.jackson.annotation.JsonIgnore;

public class BaseApiResponse extends ObjectInfo {

    /**
     * 响应状态码
     */
    protected Integer code = AnneSignConst.RESPONSE_OK_CODE;

    /**
     * 返回描述
     */
    protected String detail = AnneSignConst.RESPONSE_OK_DETAIL;

    /**
     * uuid
     */
    protected String uuid;

    /**
     * 版本号
     */
    protected String version = AnneSignConst.API_VERSION_1;

    /**
     * 响应时间
     */
    protected Long timestamp;


    public BaseApiResponse() {
    }

    /**
     * @param systemErrorCode
     */
    public BaseApiResponse(SystemErrorCode systemErrorCode) {
        this.code = systemErrorCode.getCode();
        this.detail = systemErrorCode.getDetail();
    }

    /**
     * @param systemErrorCode
     * @param detail
     */
    public BaseApiResponse(SystemErrorCode systemErrorCode, String detail) {
        this.code = systemErrorCode.getCode();
        this.detail = detail;
    }

    /**
     * @param systemErrorCode
     * @param e
     */
    public BaseApiResponse(SystemErrorCode systemErrorCode, Exception e) {
        this.code = systemErrorCode.getCode();
        this.detail = e.getMessage();
    }

    /**
     * @param code
     * @param detail
     */
    public BaseApiResponse(Integer code, String detail) {
        this.code = code;
        this.detail = detail;
    }

    @Override
    public boolean validate() throws ValidateException {
        try {
            return ValidateUtils.validate(this);
        } catch (ValidateException e) {
            throw new ValidateException(e.getMessage(), e);
        }
    }

    /**
     * 响应是否正确
     *
     * @return
     */
    @JsonIgnore
    public Boolean isOk() {
        return AnneSignConst.RESPONSE_OK_CODE.equals(this.code);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getDetail() {
        return detail;
    }

    public void setDetail(String detail) {
        this.detail = detail;
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public Long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Long timestamp) {
        this.timestamp = timestamp;
    }

}
